extends Node
class_name CharacterSkillComponent

## 运行时角色实际持有的AttributeSet实例 (通过模板duplicate而来)
var _active_attribute_set: SkillAttributeSet = null		

## 角色标签系统，用于控制角色可执行的动作类型
var _restricted_action_tags: Array[StringName] = []
## 状态字典, Key: status_id (StringName), Value: SkillStatusData (运行时实例)
var _active_statuses: Dictionary = {}
## 可用的技能
var _skills: Array[SkillData] = []

## 当状态效果被应用到角色身上时发出
signal status_applied(status_instance: SkillStatusData)							
## 当状态效果从角色身上移除时发出
signal status_removed(status_id: StringName, status_instance_data_before_removal: SkillStatusData)
## 当状态效果更新时发出 (例如 stacks 或 duration 变化)
signal status_updated(status_instance: SkillStatusData, old_stacks: int, old_duration: int)
## 属性基础值改变
signal attribute_base_value_changed(attribute_instance: SkillAttribute, old_value: float, new_value: float, source: Variant)
## 属性当前值改变
signal attribute_current_value_changed(attribute_instance: SkillAttribute, old_value: float, new_value: float, source: Variant)
## 角色标签系统改变
signal action_tags_changed(restricted_tags: Array[StringName])

## 初始化组件
func initialize(attribute_set_resource: SkillAttributeSet, skills: Array[SkillData]) -> void:
	# 这是因为AttributeSet本身是一个Resource, 直接使用会导致所有实例共享数据
	_active_attribute_set = attribute_set_resource.duplicate(true)
	# 初始化技能列表
	_skills = skills.duplicate(true)
	if not _active_attribute_set:
		push_error("无法初始化AttributeSet，资源无效！")
		return

	# 初始化AttributeSet，这将创建并配置所有属性实例
	_active_attribute_set.initialize_set()
	
	_active_attribute_set.current_value_changed.connect(_on_attribute_current_value_changed)
	_active_attribute_set.base_value_changed.connect(_on_attribute_base_value_changed)
	
	# 连接游戏事件信号
	SkillSystem.game_event_occurred.connect(_on_game_event_occurred)

#region --- 属性管理 ---
## 获取属性基础值
func get_base_value(attribute_name: StringName) -> float:
	return _active_attribute_set.get_base_value(attribute_name)

## 获取属性当前值
func get_current_value(attribute_name: StringName) -> float:
	return _active_attribute_set.get_current_value(attribute_name)

## 设置属性当前值
func set_current_value(attribute_name: StringName, value: float, source: Variant = null) -> void:
	_active_attribute_set.set_current_value(attribute_name, value, source)

## 设置属性基础值
func set_base_value(attribute_name: StringName, value: float, source: Variant = null) -> void:
	_active_attribute_set.set_base_value(attribute_name, value, source)

## 添加属性修改器
func add_attribute_modifier(attribute_name: StringName, modifier: SkillAttributeModifier) -> void:
	_active_attribute_set.add_attribute_modifier(attribute_name, modifier)

## 移除属性修改器
func remove_attribute_modifier(attribute_name: StringName, modifier: SkillAttributeModifier) -> void:
	_active_attribute_set.remove_attribute_modifier(attribute_name, modifier)

## 获取属性修改器
func get_attribute_modifiers(attribute_name: StringName) -> Array[SkillAttributeModifier]:
	return _active_attribute_set.get_attribute_modifiers(attribute_name)

## 获取属性实例
func get_attribute(attribute_name: StringName) -> SkillAttribute:
	return _active_attribute_set.get_attribute(attribute_name)

## 获取AttributeSet
func get_attribute_set() -> SkillAttributeSet:
	return _active_attribute_set

## 使用MP
func use_mp(amount: float, source: Variant = null) -> bool:
	var current_mp = _active_attribute_set.get_current_value(&"CurrentMana")
	if current_mp < amount:
		return false
	_active_attribute_set.set_current_value(&"CurrentMana", current_mp - amount, source)
	return true

## 恢复MP
func restore_mp(amount: float, source: Variant = null) -> float:
	var current_mp = _active_attribute_set.get_current_value(&"CurrentMana")
	_active_attribute_set.set_current_value(&"CurrentMana", current_mp + amount, source)
	return amount

## 消耗生命值
func consume_hp(amount: float, source: Variant = null) -> bool:
	var current_hp = _active_attribute_set.get_current_value(&"CurrentHealth")
	_active_attribute_set.set_current_value(&"CurrentHealth", current_hp - amount, source)
	return true

## 恢复生命值
func restore_hp(amount: float, source: Variant = null) -> float:
	var current_hp = _active_attribute_set.get_current_value(&"CurrentHealth")
	_active_attribute_set.set_current_value(&"CurrentHealth", current_hp + amount, source)
	return amount
#endregion

#region --- 技能管理 ---
## 尝试执行技能
## [param context] 技能执行上下文
## [param caster] 施法者
## [param skill_data] 技能数据
## [param selected_targets] 选中的目标
## [return] 执行结果
func attempt_execute_skill(skill_data: SkillData, selected_targets: Array[Character], context: SkillSystem.SkillExecutionContext) -> Dictionary:
	var caster: Character = get_parent() as Character
	# 播放攻击动画
	if not skill_data.cast_animation.is_empty():
		await caster.play_animation(skill_data.cast_animation)
	
	# 连接信号以捕获伤害值
	var damage_result : Dictionary = {}
	var damage_signal_connection = null
	
	# 创建一个临时函数来捕获伤害值
	var capture_damage = func(effect_type, _source, _target, effect_result):
		if effect_type == SkillEffectData.EffectType.DAMAGE and effect_result.has("damage"):
			damage_result["damage"] += effect_result["damage"]
			print_rich("[color=yellow]捕获到伤害值: %d[/color]" % effect_result["damage"])
	
	# 连接信号
	damage_signal_connection = SkillSystem.effect_processed.connect(capture_damage)
	
	# 调用SkillSystem的相应方法
	var success := SkillSystem.attempt_execute_skill(context, caster, skill_data, selected_targets)
	
	# 构建结果字典
	var result = {
		"success": success,
		"skill": skill_data,
		"targets": selected_targets,
		"damage": 0  # 默认伤害值为0
	}
	
	# 如果成功，等待一帧以确保效果处理开始
	if success and Engine.get_main_loop():
		await Engine.get_main_loop().process_frame
		# 等待额外的时间以确保所有效果处理完成
		await get_tree().create_timer(0.2).timeout
	
	# 断开信号连接
	if damage_signal_connection:
		SkillSystem.effect_processed.disconnect(damage_signal_connection)
	
	# 设置最终伤害值
	var damage_value : float = damage_result.get("damage", 0)
	result["damage"] = damage_value
	print_rich("[color=green]最终伤害值: %d[/color]" % damage_value)
	
	return result

## 是否有足够的MP释放技能
func has_enough_mp_for_any_skill() -> bool:
	var current_mp = _active_attribute_set.get_current_value(&"CurrentMana")
	for skill in _skills:
		if current_mp >= skill.mp_cost:
			return true
	return false

## 检查是否有足够的MP使用指定技能
func has_enough_mp_for_skill(skill: SkillData) -> bool:
	var current_mp = _active_attribute_set.get_current_value(&"CurrentMana")
	return current_mp >= skill.mp_cost

## 获取所有技能
## [return] 技能数据数组
func get_available_skills() -> Array[SkillData]:
	return _skills

## 添加技能
func add_skill(skill: SkillData) -> void:
	_skills.append(skill)

## 移除技能
func remove_skill(skill: SkillData) -> void:
	_skills.erase(skill)

## 获取技能
## [param skill_id] 技能ID
## [return] 技能数据
func get_skill(skill_id: StringName) -> SkillData:
	for skill in _skills:
		if skill.skill_id == skill_id:
			return skill
	return null

## 检查是否有指定技能
## [param skill_id] 技能ID
## [return] 是否有指定技能
func has_skill(skill_id: StringName) -> bool:
	return get_skill(skill_id) != null

## 获取技能数量
## [return] 技能数量
func get_skill_count() -> int:
	return _skills.size()
#endregion

#region --- 状态管理 ---
## 获取所有活跃状态
func get_active_statuses() -> Dictionary:
	return _active_statuses

## 获取当前的动作限制标签
func get_restricted_action_tags() -> Array[StringName]:
	return _restricted_action_tags

## 检查是否可以执行特定类别的动作
func can_perform_action_category(category: StringName) -> bool:
	return not _restricted_action_tags.has(category) and not _restricted_action_tags.has(&"any_action")

## 检查技能是否可用（基于动作限制）
func is_skill_available(skill: SkillData) -> bool:
	# 检查是否有任何动作限制标签与技能的动作类别冲突
	for category in skill.action_categories:
		if _restricted_action_tags.has(StringName(category)):
			return false
	
	# 检查是否有通用限制
	if _restricted_action_tags.has(&"any_action") or _restricted_action_tags.has(&"any_skill"):
		return false
	
	return true

## 应用状态
func apply_status(status_template: SkillStatusData, source: Character, effect_data: SkillEffectData = null) -> Dictionary:
	var result = {"applied_successfully": false, "status_instance": null, "reason": "unknown"}
	
	if not status_template:
		result.reason = "invalid_status_template"
		return result
	
	var status_id = status_template.status_id
	var duration_override = -1 # 默认使用状态模板中的持续时间
	var stacks_to_apply = 1 # 默认添加一层
	
	# 如果提供了effect_data，则使用其中的参数
	if effect_data:
		if effect_data.status_duration_override > 0:
			duration_override = effect_data.status_duration_override
		stacks_to_apply = effect_data.status_stacks_to_apply
	
	# 检查是否已经存在相同的状态
	if _active_statuses.has(status_id):
		# 更新已存在的状态
		var updated_status = _update_existing_status(status_template, source, duration_override, stacks_to_apply, result)
		if updated_status:
			result.status_instance = updated_status
			result.applied_successfully = true
			result.reason = "updated"
			return result
	
	# 处理状态覆盖机制
	_handle_status_override(status_template)
	
	# 检查是否被其他状态抗性
	if _check_status_resistance(status_template, result):
		return result
	
	# 创建新状态
	var runtime_status_instance = _apply_new_status(status_template, source, duration_override, stacks_to_apply, result)
	
	# 应用状态的动作限制标签
	if not runtime_status_instance.restricted_action_categories.is_empty():
		_add_action_restrictions(runtime_status_instance.restricted_action_categories)
	
	result.status_instance = runtime_status_instance
	result.applied_successfully = true
	result.reason = "applied"
	
	return result

## 移除状态效果
func remove_status(status_id: StringName, trigger_end_effects: bool = true) -> bool:
	if not _active_statuses.has(status_id):
		return false
	
	var runtime_status_instance = _active_statuses[status_id]
	
	# 移除属性修饰符
	_apply_attribute_modifiers_for_status(runtime_status_instance, false)
	
	# 移除状态的动作限制
	if not runtime_status_instance.restricted_action_categories.is_empty():
		_remove_action_restrictions(runtime_status_instance.restricted_action_categories)
	
	# 如果需要触发结束效果
	if trigger_end_effects and not runtime_status_instance.end_effects.is_empty():
		print_rich("[color=purple]触发 %s 的状态 %s 的结束效果[/color]" % [owner.character_name, runtime_status_instance.status_name])
		var result = await SkillSystem.process_status_end_effects(runtime_status_instance, owner)
		if not result.success:
			push_warning("Failed to process end effects for status %s: %s" % [runtime_status_instance.status_name, result.get("error", "unknown error")])
	
	# 移除状态
	_active_statuses.erase(status_id)
	
	# 发出状态移除信号
	status_removed.emit(status_id, runtime_status_instance)
	
	print_rich("[color=red]%s 的状态 %s 被移除[/color]" % [owner.character_name, runtime_status_instance.status_name])
	
	return true

## 处理状态效果并更新持续时间（在回合结束时调用）
func process_status_effects() -> void:
	var expired_status_ids: Array[StringName] = []
	
	# 处理所有状态
	for status_id in _active_statuses:
		var status : SkillStatusData = _active_statuses[status_id]
		
		# 处理持续效果
		if not status.ongoing_effects.is_empty():
			print_rich("[color=cyan]处理 %s 的状态 %s 的持续效果[/color]" % [owner.character_name, status.status_name])
			var result = await SkillSystem.process_status_ongoing_effects(status, owner)
			if not result.success:
				push_warning("Failed to process ongoing effects for status %s: %s" % [status.status_name, result.get("error", "unknown error")])
		
		# 如果状态是永久的，跳过持续时间更新
		if status.is_permanent:
			continue
		
		# 减少持续时间
		status.remaining_duration -= 1
		
		# 检查是否过期
		if status.remaining_duration <= 0:
			expired_status_ids.append(status_id)
		else:
			print_rich("[color=yellow]%s 的状态 %s 剩余持续时间: %d[/color]" % [owner.character_name, status.status_name, status.remaining_duration])
	
	# 处理过期的状态
	for status_id in expired_status_ids:
		var status = _active_statuses[status_id]
		print_rich("[color=orange]%s 的状态 %s 已过期[/color]" % [owner.character_name, status.status_name])
		
		# 移除状态 (结束效果在remove_status中处理)
		remove_status(status_id)

## 获取状态实例
func get_status(status_id: StringName) -> SkillStatusData:
	return _active_statuses.get(status_id)

## 检查是否有指定状态
func has_status(status_id: StringName) -> bool:
	return _active_statuses.has(status_id)

## 获取指定状态的层数
func get_status_stacks(status_id: StringName) -> int:
	if not _active_statuses.has(status_id):
		return 0
	return _active_statuses[status_id].stacks

## 获取所有当前活跃的状态效果
## [return] 所有当前活跃的状态效果数组
func get_all_active_statuses() -> Array[SkillStatusData]:
	var result: Array[SkillStatusData] = []
	for status_id in _active_statuses:
		result.append(_active_statuses[status_id])
	return result
#endregion

#region --- 私有方法 ---
## 私有方法：应用或移除属性修饰符
## [param runtime_status_inst] 运行时状态实例
## [param add] 是否添加修饰符
func _apply_attribute_modifiers_for_status(runtime_status_inst: SkillStatusData, add: bool = true) -> void:
	if not _active_attribute_set or not is_instance_valid(runtime_status_inst): 
		push_error("_apply_attribute_modifiers_for_status: _active_attribute_set or runtime_status_inst is invalid")
		return
	if runtime_status_inst.attribute_modifiers.is_empty(): return
	
	for modifier_template: SkillAttributeModifier in runtime_status_inst.attribute_modifiers:
		# 创建运行时修饰符实例
		var runtime_modifier : SkillAttributeModifier = modifier_template.duplicate()
		# TODO 是否受层数影响
		if runtime_status_inst.stacks > 0:
			runtime_modifier.magnitude *= runtime_status_inst.stacks
		
		# 应用或移除修饰符
		if add:
			_active_attribute_set.apply_modifier(runtime_modifier, runtime_status_inst)
		else:
			_active_attribute_set.remove_modifiers_by_source(runtime_status_inst)

## 私有方法：检查状态抵抗
func _check_status_resistance(status_template: SkillStatusData, result_info: Dictionary) -> bool:
	# 遍历所有当前已有的状态，检查是否有状态会抵抗即将应用的状态
	for status_id in _active_statuses:
		var active_status : SkillStatusData = _active_statuses[status_id]
		if active_status.resisted_by_states.has(status_template.status_id):
			result_info["applied_successfully"] = false
			result_info["reason"] = "resisted_by_status"
			result_info["resisted_by"] = active_status.status_id
			
			print(owner.character_name + " 的状态 " + active_status.status_name + " 抵抗了 " + status_template.status_name)
			return true
	
	return false

## 私有方法：处理状态覆盖
func _handle_status_override(status_template: SkillStatusData) -> void:
	if not status_template.overrides_states.is_empty():
		var ids_to_remove_due_to_override: Array[StringName] = []
		
		# 检查此状态会覆盖哪些已有状态
		for status_id in _active_statuses:
			if status_template.overrides_states.has(status_id):
				ids_to_remove_due_to_override.append(status_id)
		
		# 移除被覆盖的状态
		for status_id in ids_to_remove_due_to_override:
			var status_to_remove = _active_statuses[status_id]
			print(status_template.status_name + " 覆盖了状态 " + status_to_remove.status_name)
			remove_status(status_id)

## 私有方法：添加动作限制
## [param categories] 动作限制类别
func _add_action_restrictions(categories: Array[StringName]) -> void:
	var changed = false
	for category in categories:
		if not _restricted_action_tags.has(category):
			_restricted_action_tags.append(category)
			changed = true
	
	# 发出动作限制改变信号
	if changed:
		print_rich("[color=orange]%s 添加动作限制标签: %s[/color]" % [get_parent().character_name, categories])
		action_tags_changed.emit(_restricted_action_tags)

## 私有方法：移除动作限制
## [param categories] 动作限制类别
func _remove_action_restrictions(categories: Array[StringName]) -> void:
	var changed = false
	for category in categories:
		if _restricted_action_tags.has(category):
			_restricted_action_tags.erase(category)
			changed = true
	
	# 发出动作限制改变信号
	if changed:
		print_rich("[color=green]%s 移除动作限制标签: %s[/color]" % [get_parent().character_name, categories])
		action_tags_changed.emit(_restricted_action_tags)

## 私有方法：更新已存在的状态
## 处理状态的各种叠加行为，如刷新持续时间、增加层数等
## [param status_template] 状态模板
## [param p_source_char] 状态来源角色
## [param duration_override] 持续时间覆盖
## [param stacks_to_apply] 要应用的层数
## [param result_info] 结果信息字典
## [return] 更新后的状态实例
func _update_existing_status(
		status_template: SkillStatusData, p_source_char: Character, 
		duration_override: int, stacks_to_apply: int, result_info: Dictionary) -> SkillStatusData:
	var status_id: StringName = status_template.status_id
	var runtime_status_instance: SkillStatusData = _active_statuses[status_id]
	var old_stacks: int = runtime_status_instance.stacks
	var old_duration: int = runtime_status_instance.remaining_duration
	
	runtime_status_instance.source_character = p_source_char
	var new_duration_base = duration_override if duration_override > -1 else status_template.duration
	var new_stack_count = runtime_status_instance.stacks

	# 根据不同的堆叠行为处理状态
	match status_template.stack_behavior:
		SkillStatusData.StackBehavior.NO_STACK:
			runtime_status_instance.remaining_duration = new_duration_base
			result_info.reason = "no_stack_refreshed"
		SkillStatusData.StackBehavior.REFRESH_DURATION:
			runtime_status_instance.remaining_duration = new_duration_base
			result_info.reason = "duration_refreshed"
		SkillStatusData.StackBehavior.ADD_DURATION:
			runtime_status_instance.remaining_duration += new_duration_base
			result_info.reason = "duration_added"
		SkillStatusData.StackBehavior.ADD_STACKS_REFRESH_DURATION:
			new_stack_count = min(old_stacks + stacks_to_apply, status_template.max_stacks)
			runtime_status_instance.remaining_duration = new_duration_base
			result_info.reason = "stacked_duration_refreshed"
		SkillStatusData.StackBehavior.ADD_STACKS_INDEPENDENT_DURATION:
			new_stack_count = min(old_stacks + stacks_to_apply, status_template.max_stacks)
			runtime_status_instance.remaining_duration = max(runtime_status_instance.remaining_duration, new_duration_base)
			result_info.reason = "stacked_independent_simplified"
	
	# 如果层数变化，需要重新应用属性修改器
	if runtime_status_instance.stacks != new_stack_count:
		_apply_attribute_modifiers_for_status(runtime_status_instance, false) # 先移除旧修改器
		runtime_status_instance.stacks = new_stack_count
		_apply_attribute_modifiers_for_status(runtime_status_instance) # 再应用新修改器
	
	result_info.applied_successfully = true
	
	# 如果状态有变化，发出信号
	if old_stacks != runtime_status_instance.stacks or old_duration != runtime_status_instance.remaining_duration:
		status_updated.emit(runtime_status_instance, old_stacks, old_duration)
	
	return runtime_status_instance

## 私有方法：应用新状态
func _apply_new_status(status_template: SkillStatusData, source: Character, duration_override: int, stacks_to_apply: int, result_info: Dictionary) -> SkillStatusData:
	# 创建运行时状态实例（克隆模板）
	var runtime_status_instance: SkillStatusData = status_template.duplicate(true)
	
	# 设置源角色引用
	runtime_status_instance.source_character = source
	
	# 设置堆叠层数
	if runtime_status_instance.max_stacks > 0:
		runtime_status_instance.stacks = mini(stacks_to_apply, runtime_status_instance.max_stacks)
	else:
		runtime_status_instance.stacks = stacks_to_apply
	
	# 设置持续时间
	if duration_override > 0:
		runtime_status_instance.remaining_duration = duration_override
	else:
		runtime_status_instance.remaining_duration = runtime_status_instance.base_duration
	
	# 将状态添加到活跃状态字典
	_active_statuses[runtime_status_instance.status_id] = runtime_status_instance
	
	# 应用属性修饰符
	_apply_attribute_modifiers_for_status(runtime_status_instance, true)
	
	result_info["applied_successfully"] = true
	result_info["reason"] = "new_status_applied"
	
	print("%s 获得状态: %s (%d层，持续%d回合)" % [
		owner.to_string(),
		runtime_status_instance.status_name,
		runtime_status_instance.stacks,
		runtime_status_instance.remaining_duration
	])
	
	# 发出状态应用信号
	status_applied.emit(runtime_status_instance)
	
	return runtime_status_instance

## 私有方法：检查状态触发次数限制
func _can_status_trigger(status: SkillStatusData) -> bool:
	if status.trigger_turns > 0 and status.current_turn_trigger_count >= status.trigger_turns:
		return false
	if status.trigger_count > 0 and status.current_total_trigger_count >= status.trigger_count:
		return false
	return true

## 私有方法：更新状态触发次数
func _update_status_trigger_counts(status: SkillStatusData) -> void:
	status.current_turn_trigger_count += 1
	status.current_total_trigger_count += 1
	
	# 检查是否达到最大触发次数
	if status.trigger_count > 0 and status.current_total_trigger_count >= status.trigger_count:
		print_rich("[color=orange]状态 %s 已达到最大触发次数 %d，将被移除[/color]" % [status.status_name, status.trigger_count])
		# 使用延迟调用移除状态，避免在遍历过程中修改集合
		call_deferred("remove_status", status.status_id)

## 重置状态触发计数
func reset_status_trigger_counts() -> void:
	# 重置所有状态的回合触发计数
	for status_id in _active_statuses.keys():
		var status = _active_statuses[status_id]
		status.current_turn_trigger_count = 0

## 处理回合开始时的状态更新
func process_turn_start() -> void:
	# 更新所有状态的持续时间
	var expired_status_ids: Array[StringName] = []
	
	for status_id in _active_statuses.keys():
		var status = _active_statuses[status_id]
		
		if status.duration_type == SkillStatusData.DurationType.TURNS:
			status.remaining_duration -= 1
			if status.remaining_duration <= 0:
				expired_status_ids.append(status_id)
			else:
				# 触发回合开始事件
				SkillSystem.trigger_game_event(&"on_turn_start", {"character": get_parent()})
	
	# 移除过期状态
	for status_id in expired_status_ids:
		remove_status(status_id)
		
	await get_tree().process_frame
#endregion

func _on_attribute_base_value_changed(attribute_instance: SkillAttribute, _old_value: float, _new_value: float, _source: Variant):
	attribute_base_value_changed.emit(attribute_instance, _old_value, _new_value, _source)

func _on_attribute_current_value_changed(attribute_instance: SkillAttribute, _old_value: float, _new_value: float, _source: Variant):
	attribute_current_value_changed.emit(attribute_instance, _old_value, _new_value, _source)

## 处理游戏事件触发的状态效果
func _on_game_event_occurred(event_type: StringName, context: Dictionary) -> void:
	# 检查事件是否与当前角色相关
	var character = get_parent() as Character
	if not is_instance_valid(character):
		return
	
	# 检查事件上下文中的目标或源是否为当前角色
	var is_related_to_character = false
	if context.has("source_character") and context["source_character"] == character:
		is_related_to_character = true
	elif context.has("target_character") and context["target_character"] == character:
		is_related_to_character = true
	elif context.has("character") and context["character"] == character:
		is_related_to_character = true
	
	# 如果事件与当前角色无关，则返回
	if not is_related_to_character:
		return
	
	# 遍历所有状态，检查是否有可触发的状态
	for status_id in _active_statuses:
		var status = _active_statuses[status_id]
		if status.can_trigger_on_event(event_type):
			# 检查触发次数限制
			if not _can_status_trigger(status):
				print_rich("[color=gray]状态 %s 触发次数已达上限[/color]" % [status.status_name])
				continue
			
			print_rich("[color=orange]状态 %s 响应事件 %s[/color]" % [status.status_name, event_type])
			
			# 创建执行上下文
			var execution_context = {
				"source_character": status.source_character,
				"primary_target": character,
				"status_data": status,
				"original_event_context": context
			}
			
			# 执行触发效果
			var trigger_effects = status.get_trigger_effects()
			if not trigger_effects.is_empty():
				# 注意：第四个参数是 skill_data，第五个参数才是 context
				# 使用下划线前缀标记未使用的变量
				var _effect_result = await SkillSystem.apply_effects(status.source_character, character, trigger_effects, null, execution_context)
				
				# 更新触发计数
				_update_status_trigger_counts(status)
